package com.zcg.blogger.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.hfut.dmic.webcollector.crawler.BreadthCrawler;
import cn.edu.hfut.dmic.webcollector.crawler.Crawler;
import cn.edu.hfut.dmic.webcollector.crawler.MultiExtractorCrawler;
import cn.edu.hfut.dmic.webcollector.extract.ExtractorParams;

import com.zcg.blogger.dao.CSDNArticleDao;
import com.zcg.blogger.model.CSDNArticle;
import com.zcg.blogger.service.CSDNService;
import com.zcg.blogger.webCollector.Extractor.CSDNArticleExtractor;
import com.zcg.blogger.webCollector.Extractor.CSDNHotArticleExtractor;

@Service("CSDNService")
public class CSDNServiceImpl implements CSDNService{
	
	@Resource(name="crawler")
	private Crawler crawler;
	
	@Resource
	private CSDNArticleDao CSDNArticleDao;

	@Override
	public void saveHotArticle() throws Exception {
		crawler.addSeed("http://blog.csdn.net/ranking.html");
        ((BreadthCrawler) crawler).addRegex("http://blog.csdn.net/[a-zA-Z0-9]+/article/details/[0-9]+");
        ExtractorParams params = new ExtractorParams();
        params.put("CSDNArticleDao", CSDNArticleDao);
        /*加载抽取器时需要给出适用的url正则，在遇到满足url正则的网页时，抽取器就会在网页上执行*/
        ((MultiExtractorCrawler) crawler).addExtractor("http://blog.csdn.net/[a-zA-Z0-9]+/article/details/[0-9]+", CSDNHotArticleExtractor.class,params);

        /*同一个url可以执行多个Extractor，HtmlExtractor会在所有页面上执行*/
        /*HtmlExtractor这个抽取器需要额外的参数指定存放html文件的路径*/
        /*ExtractorParams继承自HashMap，可以使用put方法添加参数*/
//        crawler.addExtractor(".*", HtmlExtractor.class, new ExtractorParams("path", "download/html"));

        crawler.setThreads(5);
        crawler.start(5);
	}

	@Override
	public void saveArticle() throws Exception {
		crawler.addSeed("http://www.csdn.net/tag/java");
        ((BreadthCrawler) crawler).addRegex("http://blog.csdn.net/[a-zA-Z0-9]+/article/details/[0-9]+");
        ExtractorParams params = new ExtractorParams();
        params.put("CSDNArticleDao", CSDNArticleDao);
        /*加载抽取器时需要给出适用的url正则，在遇到满足url正则的网页时，抽取器就会在网页上执行*/
        ((MultiExtractorCrawler) crawler).addExtractor("http://blog.csdn.net/[a-zA-Z0-9]+/article/details/[0-9]+", CSDNArticleExtractor.class,params);

        /*同一个url可以执行多个Extractor，HtmlExtractor会在所有页面上执行*/
        /*HtmlExtractor这个抽取器需要额外的参数指定存放html文件的路径*/
        /*ExtractorParams继承自HashMap，可以使用put方法添加参数*/
//        crawler.addExtractor(".*", HtmlExtractor.class, new ExtractorParams("path", "download/html"));

        crawler.setThreads(5);
        crawler.start(5);
	}
	
	@Override
	public List<CSDNArticle> findTop10() {
		return CSDNArticleDao.findHot();
	}
	
}
